package com.zhnc.dangjianpingtaiapp.gui.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;

import com.wangdh.utilslibrary.utils.logger.TLog;
import com.zhnc.dangjianpingtaiapp.R;
import com.zhnc.dangjianpingtaiapp.action.ActionUpdataInteface;
import com.zhnc.dangjianpingtaiapp.argum.ItemGridRely;
import com.zhnc.dangjianpingtaiapp.gui.base.BaseTitleFragment;
import com.zhnc.dangjianpingtaiapp.utils.recycler.RecyclerCommonAdapter;
import com.zhnc.dangjianpingtaiapp.utils.recycler.RecyclerViewHolder;
import com.zhnc.dangjianpingtaiapp.utils.recycler.grid.HorizontalPageLayoutManager;
import com.zhnc.dangjianpingtaiapp.utils.recycler.grid.PagingItemDecoration;
import com.zhnc.dangjianpingtaiapp.utils.recycler.grid.PagingScrollHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangdh
 * @time 2019/4/2 15:12
 * @describe
 */
public class ItemGridFragment extends BaseTitleFragment implements ActionUpdataInteface {
//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.fragment_item_grid, container, false);
//    }

    private RecyclerView rv_item_grid;
    private RecyclerView rv_demo;

    private ItemGridRely rely;

    public void setRely(ItemGridRely msg) {
        setTitleRely(msg);
        rely = msg;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.fragment_item_grid);
        super.onActivityCreated(savedInstanceState);
        if (rely != null) {
            rely.requestData(this);
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                update();
            }
        }.execute();

    }

    private void update() {
        FragmentActivity activity = this.getActivity();
        if (activity == null) {
            TLog.l("上下文问null");
            return;
        }
        TLog.e("执行update");
        rv_demo = getView().findViewById(R.id.rv_item_grid);
        final List<String> data = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            data.add("itme" + i);
        }

//        rv_demo = rv_item_grid;
        RecyclerCommonAdapter<String> adapter = new RecyclerCommonAdapter<String>(activity, R.layout.item_grid_btn, data) {
            @Override
            public void updataUI(RecyclerViewHolder recyclerViewHolder, String s) {
                recyclerViewHolder.setText(R.id.itme_test_text, "更新" + s);
            }
        };
        if (rely != null) {
            rv_demo.setAdapter(rely.getAdapter());
        } else {
            rv_demo.setAdapter(adapter);
        }

        PagingScrollHelper scrollHelper = new PagingScrollHelper();
        scrollHelper.setUpRecycleView(rv_demo);
        scrollHelper.setOnPageChangeListener(new PagingScrollHelper.onPageChangeListener() {
            @Override
            public void onPageChange(int index) {
                TLog.e("更新" + index);
            }
        });
        rv_demo.setHorizontalScrollBarEnabled(true);

        //获取总页数,采用这种方法才能获得正确的页数。否则会因为RecyclerView.State 缓存问题，页数不正确。
        rv_demo.post(new Runnable() {
            @Override
            public void run() {
//                tv_page_total.setText("共" + scrollHelper.getPageCount() + "页");
            }
        });

        HorizontalPageLayoutManager layoutManager = new HorizontalPageLayoutManager(2, 4);
        RecyclerView.ItemDecoration itemDecoration = new PagingItemDecoration(this.getActivity(), layoutManager);


        rv_demo.setLayoutManager(layoutManager);
//        recyclerView.removeItemDecoration(lastItemDecoration);
//        rv_demo.addItemDecoration(itemDecoration);
        scrollHelper.updateLayoutManger();
        scrollHelper.scrollToPosition(0);
//        lastItemDecoration = itemDecoration;
    }

    @Override
    public void updata(List msg) {
        update();
    }
}
